package com.gdut.simple.presenter;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.NetworkUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.GankContract;
import com.gdut.simple.data.entity.Gank;
import com.gdut.simple.model.IGankModel;
import com.gdut.simple.model.impl.GankModelImpl;

import java.util.List;

public class GankPresenter implements GankContract.Presenter {


    private GankContract.View mGankView;
    private IGankModel mGankModel;

    public GankPresenter(GankContract.View view) {
        this.mGankView = view;
        mGankModel = new GankModelImpl();
        mGankView.setPresenter(this);
    }

    @Override
    public void refresh() {
        mGankView.showRefreshProgress();
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mGankView.hideRefreshProgress();
            mGankView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        mGankModel.refresh(mGankView.getType(), 10, new RequestCallback<List<Gank>>() {
            @Override
            public void onSuccess(List<Gank> list) {
                mGankView.getItemList().clear();
                mGankView.getItemList().addAll(list);
                mGankView.refreshList();
                mGankView.reset();
                mGankView.hideRefreshProgress();
            }

            @Override
            public void onFailure(String message) {
                mGankView.hideRefreshProgress();
                mGankView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_refresh_fail));
            }
        });
    }

    @Override
    public void loadMore(int page) {
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mGankView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        mGankModel.loadMore(mGankView.getType(), 10, page, new RequestCallback<List<Gank>>() {
            @Override
            public void onSuccess(List<Gank> list) {
                if (list == null || list.size() == 0) {
                    return;
                }
                mGankView.getItemList().addAll(list);
                mGankView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mGankView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_load_fail));
            }
        });
    }

}
